'use strict';
// 导入CryptoJS库
const CryptoJS = require('crypto-js');
module.exports = {
	/**
	 * XXXnameXXX
	 * @url api/pub/encrypt_test 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			db,
			_
		} = util;
		let {
			encryptData,
			sessionKey,
			js_code,
			uid
		} = data;
		let res = {
			code: 0,
			msg: ""
		};
		// 业务逻辑开始-----------------------------------------------------------
		
		let requestRes = await vk.openapi.weixin.request({
			method: "GET",
			url: "https://api.weixin.qq.com/sns/jscode2session",
			data: {
				appid: "wx06f6d4d34d8eb4f0",
				secret: "ae4e030858e5903e1d8c3567e0ad2d4b",
				js_code: js_code,
				grant_type:"authorization_code"
			}
		});
		let encryptKey = requestRes.session_key;
		encryptData.openid = requestRes.openid;
		console.log("encryptData.openid",encryptData.openid);
		let encryptInfo = encryptData;
		if (!CryptoJS) return false
		const st = Date.now()
		console.log('[Encrypt] get encryptKey', encryptKey)
		// 加密
		const encryptKey16bit = encryptKey.substr(0, 16)
		const parsedKey = CryptoJS.enc.Utf8.parse(encryptKey16bit)
		const parsedStr = CryptoJS.enc.Utf8.parse(JSON.stringify(encryptInfo));
		const encryptStr = CryptoJS.AES.encrypt(parsedStr, parsedKey, {
			mode: CryptoJS.mode.ECB,
			padding: CryptoJS.pad.Pkcs7
		});
		console.log('[Encrypt] get encrypt4', encryptStr.toString().substr(0, 100))
		res.encrypted=encryptStr.toString();
		return res;
		// 业务逻辑结束-----------------------------------------------------------
	}
}